<?php
class ProviderModel extends CI_Model {
	
	var $gallery_path;

	

	function __construct() {
		parent::__construct ();
	}
	
	function getProvider($slug = FALSE) {
		if ($slug === FALSE) {
			$query = $this->db->get ( 'provider' );
			return $query->result_array ();
		} else {
			$query = $this->db->get_where ( 'provider', array ('slug' => $slug ) );
			return $query->row_array ();
		}
	}
	
	function get($id=0,$limit=0,$offset=0){
		if(!is_numeric($id) || !is_numeric($limit) || $id < 0 || $limit < 0)
			return null;
		if($id == 0){
			if($limit > 0)
				$this->db->limit($limit,$offset);
			$query = $this->db->get('provider');
			return $query->result();
		}else{
			$this->db->where('id',$id);
			$query = $this->db->get('provider');
			if($query->num_rows() > 0)
				return $query->first_row();
		}
	}
	
	
	public function create($data) {
		foreach ( $data as &$d ) {
			$d = $this->db->escape_str ( $d );
		}
		return $this->db->insert ( 'provider', $data );

	}
	
	function delete($id) {
		$this->db->where ( 'id', $id );
		return $this->db->delete ( 'provider' );
	}
	
	function update($id, $data) {
		$this->db->where ( 'id', $id );
		$query = $this->db->get ( 'provider' );
		
		if ($query->num_rows () == 0)
			return false;
		else {
			$this->db->where ( 'id', $id );
			if ($this->db->update ( 'provider', $data ))
				return true;
			else
				return false;
		}
	}
	function getNewestId(){
		$query = mysql_query("SELECT MAX(id) FROM provider");
		$results = mysql_fetch_array($query);
		return $results['MAX(id)'];
	}

	function getbyfillter($category = 0, $pricepolicy = 0, $timepolicy = 0, $level = 0, $province = '0', $phone = '', $name = '' ){
		if(($category == 0) && ($pricepolicy == 0) && ($timepolicy == 0) && ($level == 0) && ($province == '0') && ($phone == '0') && ($name == '0')){
			return $this->getProvider ();
		}

		$sql = 'SELECT provider.id , provider.name, createDate, logo, province, address, district, phone

				FROM provider ';
				if($category != 0){
				$sql .=	'JOIN provider_category ON provider.id = provider_category.provider_id 
					JOIN category ON category_id = category.id ';
				}
				if($pricepolicy != 0){
				$sql .=	' JOIN provider_pricepolicy ON provider.id = provider_pricepolicy.provider_id 
					JOIN pricepolicy ON pricepolicy_id = pricepolicy.id ';
				}
				if($timepolicy != 0){
				$sql .=	' JOIN provider_timepolicy ON provider.id = provider_timepolicy.provider_id 
					JOIN timepolicy ON timepolicy_id = timepolicy.id ';

				}
				$sql .= ' WHERE provider.hidden = 1 ';
				if($category != 0){
				$sql .= ' AND category.id = '.$category;
				}
				if($pricepolicy != 0){
				$sql .= ' AND pricepolicy.id = '.$pricepolicy;
				}

				if($timepolicy != 0){
				$sql .= ' AND timepolicy.id = '.$timepolicy;
				}
				if($level != 0){
				$sql .= ' AND level	= '.$level;
				}
				if($province != '0'){
				$sql .= ' AND province	= "'.$province.'" ';
				}
				if($name != ''){
				$sql .= ' AND name	LIKE "%'.$name.'%"';
				}
				if($phone != ''){
				$sql .= ' AND phone	LIKE "%'.$phone . '%"';
				}



		$query = $this->db->query($sql);
		return $query -> result_array ();

	}

}